//
//  NSObject+MBProgressHUD.m
//  WBBaseProject
//
//  Created by 吴斌 on 2017/6/28.
//  Copyright © 2017年 吴斌. All rights reserved.
//

UIWindow *_mainWindow() {
    id appDelegate = [UIApplication sharedApplication].delegate;
    if (appDelegate && [appDelegate respondsToSelector:@selector(window)]) {
        return [appDelegate window];
    }
    
    NSArray *windows = [UIApplication sharedApplication].windows;
    if ([windows count] == 1) {
        return [windows firstObject];
    }
    else {
        for (UIWindow *window in windows) {
            if (window.windowLevel == UIWindowLevelNormal) {
                return window;
            }
        }
    }
    return nil;
}

#import "NSObject+MBProgressHUD.h"

#define kBlackColor  [UIColor blackColor]
#define kWhiteColor  [UIColor whiteColor]
#define kDuration 1.5

@implementation NSObject (MBProgressHUD)

/*! 获取当前屏幕的最上方正在显示的那个view */
+ (UIView *)getCurrentView{
    UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
    // vc: 导航控制器, 标签控制器, 普通控制器
    if ([vc isKindOfClass:[UITabBarController class]]) {
        vc = [(UITabBarController *)vc selectedViewController];
    }
    if ([vc isKindOfClass:[UINavigationController class]]) {
        vc = [(UINavigationController *)vc visibleViewController];
    }
    return vc.view;
}

/*!
 *  显示忙
 */
+ (void)wb_showBusy{
    [self wb_showAlert:nil];
}

/*!
 *  弹出文字提示（菊花转动）
 *
 *  @param text 提示内容
 */
+ (void)wb_showAlert:(NSString *)text{
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:_mainWindow() animated:YES];
        MBProgressHUD *progressHUD = [MBProgressHUD showHUDAddedTo:_mainWindow() animated:YES];
        progressHUD.mode = MBProgressHUDModeIndeterminate;
        progressHUD.animationType = MBProgressHUDAnimationZoom;
        progressHUD.bezelView.color = kBlackColor;
        progressHUD.contentColor = kWhiteColor;
        progressHUD.label.text = text;
    });
}

/*!
 *  弹出文字提示，自定义显示时间
 *
 *  @param text 提示内容
 *  @param duration 时间
 */
+ (void)wb_showAlertWithTitle:(NSString *)text duration:(CGFloat)duration{
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:_mainWindow() animated:YES];
        MBProgressHUD *progressHUD = [MBProgressHUD showHUDAddedTo:_mainWindow() animated:YES];
        progressHUD.mode = MBProgressHUDModeText;
        progressHUD.label.text = text;
        progressHUD.label.numberOfLines = 0;
        progressHUD.animationType = MBProgressHUDAnimationZoom;
        progressHUD.bezelView.color = kBlackColor;
        progressHUD.contentColor = kWhiteColor;
        [progressHUD showAnimated:YES];
        [progressHUD hideAnimated:YES afterDelay:duration];
    });
}

/*!
 *  弹出文字提示，自定义显示时间(默认1.5秒)
 *
 *  @param text 提示内容
 */
+ (void)wb_showAlertWithTitle:(NSString *)text{
    [self wb_showAlertWithTitle:text duration:TipTime];
}

/*!
 *  隐藏提示
 */
+ (void)wb_hideProgress{
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:_mainWindow() animated:YES];
    });
}

@end
